//related to badgeround.ui
var root = ui("$");
var app = sm("do_App");
var page = sm("do_Page");
var sysUrl = require("url/sys");
var qqHttp = require("helper/http");
var cacher = deviceone.sm("do_DataCache");
var indexCacher = require("helper/indexCacher");
//遮罩显示
var animMaskShow = mm("do_Animator");
var propsMS = {bgColor:"00000044"};
animMaskShow.append(320,propsMS,"EaseOut");
//面板显示
var animPanelShow = mm("do_Animator");
var propsPS = {x:140,alpha:1};
animPanelShow.append(320,propsPS,"EaseOut");
//遮罩隐藏
var animMaskHide = mm("do_Animator");
var propsMH = {bgColor:"00000000"};
animMaskHide.append(320,propsMH,"EaseOut");
//面板隐藏
var animPanelHide = mm("do_Animator");
var propsPH = {x:640,alpha:0.8};
animPanelHide.append(320,propsPH,"EaseOut");
//遮罩
var bgmask = ui("maskbg");
var paneler = ui("regionMain"); //修改id
var panelerClose = function(){
	bgmask.animate(animMaskHide,function(){
		bgmask.visible = false;
	});
	paneler.animate(animPanelHide);
};
bgmask.on("touch","",300, panelerClose);
//显示
root.on("regionShow",function(data){
	bgmask.visible = true;
	bgmask.animate(animMaskShow);
	paneler.animate(animPanelShow);
	//事件
	region.init();
});
root.on("regionClose",function(data){
	panelerClose();
});

var indexListView = ui("indexListView");
var cityData = mm("do_HashData");
var textFieldSearch = ui("textFieldSearch");
var hotView = ui("hotView");
var latelyView = ui("latelyView");
var curText = ui("curText");
var region = {
	hotData:[{"id":"110200000","name":"北京"},{"id":"310200000","name":"上海"},{"id":"440300000","name":"深圳"},{"id":"440100000","name":"广州"},{"id":"120200000","name":"天津"},{"id":"500200000","name":"重庆"}],
	latelyData:[],
	init:function(){
		var _selectCityId = indexCacher.selectCityId();
		var _selectCityName = indexCacher.selectCityName();
		if(_selectCityId){
			curText.text = _selectCityName;
			curText.tag = _selectCityId;
		}
		else{
			curText.text = "全国";
			curText.tag = 0;
		}
		region.loadData();
		region.getLatelyList();
		region.getHotList();
	},
	loadData:function(_keyword) {
		var param = {search:_keyword};
		qqHttp.post(sysUrl.url.area.cityMap, param, function(data) {
			if(data.cityMapKeys.length==0){
				cityData.removeAll();
				cityData.addData({
					"" : [{"template":2}]
				});
				indexListView.bindItems(cityData, [""]);
				indexListView.refreshItems();
				return;
			}
			region.addDo(data);
		})
	},
	addDo:function(data){
		cityData.removeAll();
		var firstChars = data.cityMapKeys;
		var cityMap = data.cityMap;
		for (var i = 0; i < firstChars.length; i++) {
			var firstChar = firstChars[i];
			var cityValues = cityMap[firstChar];
			var cityTmplate = [];
			cityTmplate.push({
				"template" : 1,
				"firstChar" : firstChar
			});
			for (var j = 0; j < cityValues.length; j++) {
				var cityValue = cityValues[j];
				cityTmplate.push({
					"nameAll" : cityValue.nameAll,
					"id" : cityValue.id
				});
			}
			if (firstChar == "A") {
				cityData.addData({
					"A" : cityTmplate
				});
			} else if (firstChar == "B") {
				cityData.addData({
					"B" : cityTmplate
				});
			} else if (firstChar == "C") {
				cityData.addData({
					"C" : cityTmplate
				});
			} else if (firstChar == "D") {
				cityData.addData({
					"D" : cityTmplate
				});
			} else if (firstChar == "E") {
				cityData.addData({
					"E" : cityTmplate
				});
			} else if (firstChar == "F") {
				cityData.addData({
					"F" : cityTmplate
				});
			} else if (firstChar == "G") {
				cityData.addData({
					"G" : cityTmplate
				});
			} else if (firstChar == "H") {
				cityData.addData({
					"H" : cityTmplate
				});
			} else if (firstChar == "I") {
				cityData.addData({
					"I" : cityTmplate
				});
			} else if (firstChar == "J") {
				cityData.addData({
					"J" : cityTmplate
				});
			} else if (firstChar == "K") {
				cityData.addData({
					"K" : cityTmplate
				});
			} else if (firstChar == "L") {
				cityData.addData({
					"L" : cityTmplate
				});
			} else if (firstChar == "M") {
				cityData.addData({
					"M" : cityTmplate
				});
			} else if (firstChar == "N") {
				cityData.addData({
					"N" : cityTmplate
				});
			} else if (firstChar == "O") {
				cityData.addData({
					"O" : cityTmplate
				});
			} else if (firstChar == "P") {
				cityData.addData({
					"P" : cityTmplate
				});
			} else if (firstChar == "Q") {
				cityData.addData({
					"Q" : cityTmplate
				});
			} else if (firstChar == "R") {
				cityData.addData({
					"R" : cityTmplate
				});
			} else if (firstChar == "S") {
				cityData.addData({
					"S" : cityTmplate
				});
			} else if (firstChar == "T") {
				cityData.addData({
					"T" : cityTmplate
				});
			} else if (firstChar == "U") {
				cityData.addData({
					"U" : cityTmplate
				});
			} else if (firstChar == "V") {
				cityData.addData({
					"V" : cityTmplate
				});
			} else if (firstChar == "W") {
				cityData.addData({
					"W" : cityTmplate
				});
			} else if (firstChar == "X") {
				cityData.addData({
					"X" : cityTmplate
				});
			} else if (firstChar == "Y") {
				cityData.addData({
					"Y" : cityTmplate
				});
			} else if (firstChar == "Z") {
				cityData.addData({
					"Z" : cityTmplate
				});
			}
		}
		indexListView.bindItems(cityData, firstChars);
		indexListView.refreshItems();
	},
	getLatelyList:function(){
		var _currentCityId = indexCacher.currentCityId();
		var _currentCityName = indexCacher.currentCityName();
		//最近地区
		region.latelyData = [{"id":"0","name":"全国"},{"id":_currentCityId,"name":_currentCityName}];
		latelyView.items= region.latelyData;
	},
	getHotList:function(){
		//热门地区
		hotView.items = region.hotData;
	}
};
indexListView.on("touch",function(data, e){
    var cell = cityData.getOne(data.groupID);
    var childData = cell[data.index];
    panelerClose();
    page.fire("getRegionList",{cityName:childData.nameAll,cityId:childData.id});
});
ui("layoutSearch").on("touch", function(data, e) {
	page.hideKeyboard();
	var _keyword = textFieldSearch.text;
	region.loadData(_keyword);
});
textFieldSearch.on("enter", function(data, e) {
	page.hideKeyboard();
	region.loadData(this.text);
});

hotView.on("touch",function(data){
	panelerClose();
	page.fire("getRegionList",{cityId:region.hotData[data].id,cityName:region.hotData[data].name});
});
latelyView.on("touch",function(data){
	panelerClose();
	page.fire("getRegionList",{cityId:region.latelyData[data].id,cityName:region.latelyData[data].name});
});
ui("layoutCur").on("touch",function(data){
	panelerClose();
	page.fire("getRegionList",{cityId:curText.tag,cityName:curText.text});
});